package com.sky.happygos.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.Toast;

import com.sky.happygos.R;
import com.sky.happygos.base.BaseA;
import com.sky.happygos.common.LoadView;
import com.sky.happygos.event.RegisterE;
import com.sky.happygos.present.UserP;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * Created by qqqqqqq on 2017/4/14.
 */
@EActivity(R.layout.activity_register)
public class RegisterA extends BaseA{

    @Bean
    UserP userP;
    @ViewById(R.id.et_register_phone)
    EditText phoneE;
    @ViewById(R.id.et_register_pw)
    EditText passwrodE;
    @ViewById(R.id.loadView)
    LoadView loadView;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Click(R.id.register)
    public void register(){
        String phoneStr = phoneE.getText().toString();
        try {
            long phone = Long.parseLong(phoneStr);
            String pwStr = passwrodE.getText().toString();
            loadView.start();
            userP.register(phone,pwStr);
        }catch (NumberFormatException e){
            Toast.makeText(this,"手机号码错误，或存在非法字符",Toast.LENGTH_SHORT).show();
        }
    }

    @Click(R.id.header_right)
    public void login(){
        LoginA_.intent(this).start();
    }


    @Subscribe
    public void registerResult(RegisterE e){
        loadView.dismiss();
        if(e.isSuccess){
            Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();
            LoginA_.intent(this).start();
        }else{
            Toast.makeText(this,"注册失败 error:"+e.error.message,Toast.LENGTH_SHORT).show();
        }
    }


}
